home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 223_01 / wc.c < prev    next >
Text File  |  1980-01-01  |  2KB  |  105 lines

  1. /*
  2. **    WC.C
  3. **    Written by Leor Zolman, 3/16/82
  4. **
  5. **    Modified for Small-C 2.7        by F.A.Scacchitti
  6. **                            9/23/84
  7. **
  8. **
  9. **    Text analysis utility. Given a list of text files, WC prints
  10. **    out total number of characters, words and lines in each file
  11. **    and in all files together. "Words", here, are simply delimited
  12. **    by blanks, tabs or newlines.
  13. **
  14. **    Maximum number of words and lines are each 32767, but the char
  15. **    count is virtually unlimited. ( >4E9 )
  16. **
  17. */
  18.  
  19. #include <stdio.h>
  20.  
  21. FILE ibuf;
  22. int  lo_totchars, hi_totchars, totwords, totlines;
  23.  
  24. main(argc,argv) int argc; char *argv[]; {
  25.  
  26.     lo_totchars=hi_totchars=totwords=totlines=0;       
  27.  
  28.     if(argc == 1)
  29.         {
  30.         prntf("\n\nUsage: wc <file1> <file2> . . . <fileN> <CR>\n\n");
  31.         exit();
  32.         }
  33.     else
  34.         prntf("\n\t\tchars\twords\tlines\n\n");
  35.  
  36.     while (--argc) 
  37.  
  38.         dofile(*++argv);    /* process the files */
  39.  
  40.  
  41.     prntf("\nTotals:");        /* print the results */
  42.  
  43.     if (hi_totchars) prntf("\t\t%d%04d",hi_totchars,lo_totchars);
  44.  
  45.     else prntf("\t\t%d",lo_totchars);
  46.  
  47.     prntf("\t%d\t%d\n",totwords,totlines);
  48. }
  49.  
  50. dofile(name)
  51. char *name;
  52. {
  53.     char inword;
  54.     int c;
  55.     int  lo_tch, hi_tch, twords, tlines;
  56.  
  57.     ibuf = (fopen(name,"R"));
  58.     if (ibuf == NULL)
  59.         {
  60.         prntf("Can't open %s\n",name);
  61.         return;
  62.         }
  63.     prntf("%s:\t",name);
  64.     if (strlen(name) < 7)
  65.         putchar('\t');
  66.     
  67.     inword = lo_tch = hi_tch = twords = tlines = 0;
  68.  
  69.     while ((c =fgetc(ibuf)) != EOF ) {
  70.  
  71.         if (++lo_tch == 10000) {
  72.             lo_tch = 0;
  73.             hi_tch++;
  74.         }
  75.  
  76.         if (isspace(c)) {
  77.             if (inword) {
  78.                 inword = 0;
  79.                 twords++;
  80.             }
  81.         } else
  82.             if (!inword)
  83.                 inword = 1;
  84.         
  85.         if (c == '\n')
  86.             tlines++;
  87.     }
  88.  
  89.     if (hi_tch) prntf("%d%04d",hi_tch,lo_tch);
  90.     else prntf("%d",lo_tch);
  91.     prntf("\t%d\t%d\n",twords,tlines);
  92.  
  93.     if ((lo_totchars += lo_tch) >= 10000) {
  94.         lo_totchars -= 10000;
  95.         hi_totchars++;
  96.     }
  97.     hi_totchars += hi_tch;
  98.     totwords += twords;
  99.     totlines += tlines;
  100.  
  101. fclose(ibuf);
  102. }
  103.  
  104.  
  105.